package menu

import (
	"github.com/micro-plat/hydra"
	"github.com/micro-plat/lib4go/types"
)

// GetAllMenus 获取所有可用菜单
func GetAllMenus(tenantID int) (types.XMaps, error) {
	db := hydra.C.DB().GetRegularDB()
	params := map[string]interface{}{
		"tenant_id": tenantID,
	}
	data, err := db.Query(sqlGetAllMenus, params)
	if err != nil {
		return nil, err
	}
	return data, nil
}

// FormatMenus 整理菜单结构
func FormatMenus(menus types.XMaps) []types.XMap {
	menuMap := make(map[int64]types.XMap)
	var root = make([]types.XMap, 0)
	for _, menu := range menus {
		id := menu.GetInt64("id")
		pid := menu.GetInt64("pid")
		defer delete(menu, "pid")
		menuMap[id] = menu
		if pid == 0 {
			menu.SetValue("level", 1)
			root = append(root, menu)
			continue
		}
		_, ok := menuMap[pid]
		if ok {
			childrenNode, ok := menuMap[pid].Get("children")
			if !ok {
				menuMap[pid].SetValue("children", types.XMaps{})
				childrenNode, _ = menuMap[pid].Get("children")
			}
			children := childrenNode.(types.XMaps)
			children = append(children, menu)
			menu.SetValue("level", menuMap[pid].GetInt("level")+1)
			menuMap[pid].SetValue("children", children)
		}

	}

	return root
}

// AddMenu 添加菜单
func AddMenu(tenantID int, label, path, icon string, expand bool, pid int) error {
	db := hydra.C.DB().GetRegularDB()
	params := map[string]interface{}{
		"label":     label,
		"path":      path,
		"icon":      icon,
		"expand":    types.DecodeInt(expand, true, 1, 0),
		"pid":       pid,
		"tenant_id": tenantID,
	}
	_, err := db.ExecuteBatch(sqlInsertMenu, params)
	return err
}

// DeleteMenu 删除菜单
func DeleteMenu(tenantID int, id int64) error {
	db := hydra.C.DB().GetRegularDB()
	params := map[string]interface{}{
		"id":        id,
		"tenant_id": tenantID,
	}
	_, err := db.Execute(sqlDeleteMenu, params)
	return err
}

// UpdateMenuStatus 启用/禁用菜单
func UpdateMenuStatus(tenantID int, id int64, status bool) error {
	db := hydra.C.DB().GetRegularDB()
	params := map[string]interface{}{
		"id":        id,
		"status":    types.DecodeInt(status, true, 1, 0),
		"tenant_id": tenantID,
	}
	_, err := db.Execute(sqlUpdateStatus, params)
	return err
}

// UpdateMenu 修改菜单数据
func UpdateMenu(tenantID int, id int64, label, path, icon string, expand bool) error {
	db := hydra.C.DB().GetRegularDB()
	params := map[string]interface{}{
		"id":        id,
		"label":     label,
		"path":      path,
		"icon":      icon,
		"expand":    types.DecodeInt(expand, true, 1, 0),
		"tenant_id": tenantID,
	}
	_, err := db.Execute(sqlUpdateMenu, params)
	return err
}

// MoveMenuBackward 向后移动菜单
func MoveMenuBackward(tenantID int, sourceID, targetID int64) error {
	db := hydra.C.DB().GetRegularDB()
	params := map[string]interface{}{
		"sourceId":  sourceID,
		"targetId":  targetID,
		"tenant_id": tenantID,
	}
	_, err := db.ExecuteBatch(sqlMoveMenuBackward, params)
	return err
}

// MoveMenuForward 向前移动菜单
func MoveMenuForward(tenantID int, sourceID, targetID int64) error {
	db := hydra.C.DB().GetRegularDB()
	params := map[string]interface{}{
		"sourceId":  sourceID,
		"targetId":  targetID,
		"tenant_id": tenantID,
	}
	_, err := db.ExecuteBatch(sqlMoveMenuForward, params)
	return err
}
